package com.sjzsqjy.asdtiang.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sjzsqjy.asdtiang.db.SearchService;
import com.sjzsqjy.asdtiang.search.BloodResultFormat;
import com.sjzsqjy.asdtiang.util.MyConverter;
import com.sjzsqjy.asdtiang.util.MyFile;

public class SearchAction extends ActionSupport {
	private Log log = LogFactory.getLog(SearchAction.class);
	private String type;
	private String name;
	private String dateString;
	private SearchService searchService;
	private List shiQiaoMany;
	private List traceMany;
	private String title;
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List getTraceMany() {
		return traceMany;
	}

	public void setTraceMany(List traceMany) {
		this.traceMany = traceMany;
	}

	private List bloodResult;
	private BloodResultFormat patient;

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String initExecute() throws Exception {
		System.out.println(title);
		this.title=MyConverter.getMyConverter().converter(this.title);
		if(type.equals("1")){
			title="临床检验报告单查询";
		}
		if(type.equals("2")){
			title="临床生化检验报告单查询";
		}
		if(type.equals("3")){
			title="普利生仪器中心血流变仪测试报告查询";
		}
		if(type.equals("4")){
			title="微量元素检测报告单查询";
		}
		//log.info(title);
		//log.info("search type:" + type);
		ActionContext.getContext().getSession().put("type", type);
		return SUCCESS;
	}

	public String searchExecute() throws Exception {
		//log.info("name:" + name);
		if (name.equals("") || null == dateString) {
			this.addFieldError("null", "时间或者姓名不有为空");
			return INPUT;

		}
		log.info("date" + dateString.toString());
		// //type==1表示 临床检验报告单查询即思桥软件结果查询
		if (ActionContext.getContext().getSession().get("type").equals("1")) {
			SimpleDateFormat   df   =new   SimpleDateFormat("yyyy-MM-dd");		
			Date date= df.parse(dateString);
			SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

			List l = searchService.shiQiaoUserSearch(dateString, name);
			log.info("date:" + time.format(date).split(" ")[0]);
			if (null == l) {
				this.addFieldError("noData", "没有查询到相关数据");
				return "noData";
			}
			if (l.size() > 1) {
				ActionContext.getContext().getSession()
						.put("shiQiaoUser", null);
				shiQiaoMany = l;
				return "shiQiaoMany";
			}
			log.info("date:" + time.format(date).split("")[0] + "size:"
					+ l.size());
			ActionContext.getContext().getSession()
					.put("shiQiaoUser", l.get(0));
			return "shiQiao";

		}
		// ///type=2样品软件检测报告
		if (ActionContext.getContext().getSession().get("type").equals("2")) {
			SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			log.info(time.format(dateString));
			String[] dateStringArray = dateString.split("-");
			log.info("date:" + time.format(dateString).split(" ")[0]);
			log.info(dateStringArray[0] + dateStringArray[1] + dateStringArray[2]);
			List l = searchService.sampleUser(name, dateStringArray[0]
					+ dateStringArray[1] + dateStringArray[2]);
			log.info("date:" + time.format(dateString).split(" ")[0]);
			if (null == l) {
				this.addFieldError("noData", "没有查询到相关数据");
				return "noData";
			}
			if (l.size() > 1) {
				ActionContext.getContext().getSession().put("sampleUser", null);
				shiQiaoMany = l;
				return "sampleMany";
			}
			log.info("date:" + time.format(dateString).split("")[0] + "size:"
					+ l.size());
			ActionContext.getContext().getSession().put("sampleUser", l.get(0));
			return "sampleUser";

		}
		/////血流检测报告
		if (ActionContext.getContext().getSession().get("type").equals("3")) {
			SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
			String dateString1 =ShiQiaoSearchAction.formatDate(time.format(dateString)) ;
			MyFile file = new MyFile(ServletActionContext.getServletContext()
					.getRealPath("/blood"));
			bloodResult = new ArrayList();
			String[] files = file.getFile().list();
			String fileName = "";
			Pattern pattern = Pattern.compile(dateString1+"_\\w*_"+name+".rep");
			System.out.println(":::"+dateString1+"_\\w*_"+name+".rep");
			for (int i = 0; i < files.length; i++) {
				Matcher matcher = pattern.matcher(files[i]);
				boolean b = matcher.matches();
				if (b == true) {
					fileName = files[i];
					break;
				}
			}
			if (fileName.equals("")) {
				this.addFieldError("noData", "没有查询到相关数据");
				return "noData";
			}
			List listLine = MyFile.getListFromFlie(ServletActionContext
					.getServletContext().getRealPath("/blood/" + fileName), 400,
					"gbk");
			patient = new BloodResultFormat();
			int sex = 5;
			if (((String) listLine.get(1)).contains("男")) {
				sex = 1;
			} else {
				sex = 2;
			}
			int i = 0, j = 0;
			for (; i < listLine.size(); i++)
				switch (i) {
				case 0:
					patient.setName((String) listLine.get(i));
					break;
				case 1:
					patient.setSendDate((String) listLine.get(i));
					break;
				case 2:
					patient.setTestDate((String) listLine.get(i));
					break;
				case 3:
					patient.setDoorNum((String) listLine.get(i));
					break;
				case 4:
					patient.setPatientNum((String) listLine.get(i));
					break;
				case 5:
					patient.setSex((String) listLine.get(i));
					break;
				case 6:
					patient.setSendDoctor((String) listLine.get(i));
					break;
				case 7:
					patient.setTestDoctor((String) listLine.get(i));
					break;
				case 8:
					patient.setHospitalNum((String) listLine.get(i));
					break;
				case 9:
					patient.setBedNum((String) listLine.get(i));
					break;
				case 10:
					patient.setAge((String) listLine.get(i));
					break;
				case 11:
					patient.setKeBie((String) listLine.get(i));
					break;
				case 12:
					patient.setSampleId((String) listLine.get(i));
					break;
				case 13:
					patient.setType((String) listLine.get(i));
					break;
				default:
					BloodResultFormat r = new BloodResultFormat();
					String[] str = ((String) listLine.get(i)).split(" ");
					//log.info("lineValue"+i+"  --"+listLine.get(i));
					String it = (String) listLine.get(i);
					r.setItem(r.itemList[j][0]);
					//log.info(str[0]);
					it=(String) str[0].subSequence(str[0].indexOf("：")+1, str[0].length());
				//	log.info(str[0].subSequence(str[0].indexOf("："), str[0].length()));
					try {
						r.setValue(Integer
								.parseInt(it.substring(it.length() - 5,
										it.length()).replace(".", ".")));
					} catch (Exception e) {
						try {
							r.setValue(Float.parseFloat(it));
						} catch (Exception e1) {
							try {
								r.setValue(Float.parseFloat(it));
							} catch (Exception e2) {
								r.setValue(Float.parseFloat(it));
							}
						}
					}
				//	log.info("formatValue:"+r.getValue());
					r.isNormal(j, sex);
					if(sex==1){
						r.setHight(BloodResultFormat.normalList1[j][1]+"");
						r.setLow(BloodResultFormat.normalList1[j][0]+"");
					}
					else{
						r.setHight(BloodResultFormat.normalList[j][1]+"");
						r.setLow(BloodResultFormat.normalList[j][0]+"");
					}
					j++;
					bloodResult.add(r);
				}
			return "bloodResult";
		}
		
		// render
		// view:"/bloodSearch/result",model:[resultList:resultList,patient:patient,sex:sex]
		// return
		// }
		//			
		// }
		
		
////type==4,微量元素检测报告
		if (ActionContext.getContext().getSession().get("type").equals("4")){
			SimpleDateFormat time = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
			String[] dateStringArray = dateString.split("-");
			log.info(time.format(dateString));

			List l = searchService.traceUser(name,dateStringArray[0]+dateStringArray[1]+dateStringArray[2]);
			if (null == l) {
				this.addFieldError("noData", "没有查询到相关数据");
				return "noData";
			}
			if (l.size() > 1) {
				ActionContext.getContext().getSession()
						.put("traceUser", null);
				traceMany = l;
				return "traceMany";
			}
			ActionContext.getContext().getSession()
					.put("traceUser", l.get(0));
			return "trace";
		}
//		if(session.type=="4"){
//			def patientList=TraceUser.findAllWhere(date:TraceSearchController.formatDate(params.myDate.format("yyyy-MM-dd")),name:params.name)
//			log.info "sizedddddd"+patientList.size()
//			if(!patientList){
//				patientList==TraceUser.findAllByName(params.name)
//			}
//			if(!patientList){
//				render "没有查询到相关信息"
//				return
//			}
//			if(patientList.size()>1){
//				log.info patientList[0].name
//				render view:"/traceSearch/many", model:[patientList:patientList]					
//				return
//			}
//			log.info "size"+patientList.size()
//			log.info patientList[0]
//			session.putValue "patient", patientList[0]
//			redirect( controller:"traceSearch",action:"result")
//		}
		return "search";
	}

	public List getBloodResult() {
		return bloodResult;
	}

	public void setBloodResult(List bloodResult) {
		this.bloodResult = bloodResult;
	}


	public BloodResultFormat getPatient() {
		return patient;
	}

	public void setPatient(BloodResultFormat patient) {
		this.patient = patient;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List getShiQiaoMany() {
		return shiQiaoMany;
	}

	public void setShiQiaoMany(List shiQiaoMany) {
		this.shiQiaoMany = shiQiaoMany;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDateString() {
		return dateString;
	}

	public void setDateString(String dateString) {
		this.dateString = dateString;
	}



}
